package com.zxc.controller.marketing;

/*
 * @Author: zxc of Russell
 * @Date: 2021/9/21 13:41
 * @Version 1.0
 *
 */

import com.zxc.entity.SalChance;
import com.zxc.entity.SysUser;
import com.zxc.service.SalChanceService;
import com.zxc.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/develop")
public class DevelopController {

    @Autowired
    private SalChanceService salChanceService;

    @Autowired
    private SysUserService sysUserService;

    @RequestMapping("/{skipCount}/{pageSize}")
    @ResponseBody
    public Map<String,Object> getSalChanceList(@PathVariable Integer skipCount,
                                               @PathVariable Integer pageSize,
                                               HttpSession session){
        Map<String,Object> modelMap = new HashMap<>();
        //计算出略过的记录数
        int skipCounts = (skipCount-1)*pageSize;
        //将销售计划指派给某个人执行的时候，我们进行分页，需要根据当前登录的用户来进行分页，只找出销售计划的执行人是当前用户的数据
        SysUser sysUser = (SysUser) session.getAttribute("sysUser");
        String sysUserName = sysUser.getUserName();
        //根据当前登录用户执行不同的sql
        // 如果是客户经理就只能看到属于自己的销售机会
        // 系统管理员和客户主管可以看到所有的销售机会,无论是开发中还是，已归档（开发成功、开发失败）
        //1、首先根据登录人员的名称进行查询，查询出他所对应的角色
        String sysRole = sysUserService.getSysUserRole(sysUserName);
        //2、根据角色来决定他根据哪条sql进行查询数据
        List<SalChance> salChanceList = new ArrayList<>();
        if (sysRole.equals("客户经理")){
             salChanceList = salChanceService.getSalChanceListByConditionByStatus(skipCounts,pageSize,sysUserName);
        }else {
             salChanceList = salChanceService.getAllSalChanceList(skipCounts,pageSize);
        }
        for (SalChance salChance : salChanceList){
            if (salChance.getChcStatus().equals("2")){
                //0、申请开发失败 1、正常 2、开发中 3、已归档 4、开发成功 5、开发失败
                salChance.setChcStatus("开发中");
            }else if (salChance.getChcStatus().equals("0")){
                salChance.setChcStatus("申请开发失败");
            } else {
                salChance.setChcStatus("已归档");
            }
        }

        modelMap.put("dataList",salChanceList);
        int total = salChanceService.selectCountSalChanceForMarketingByChcDueIds();
        modelMap.put("total",total);
        return modelMap;
    }

    @RequestMapping(value = "/fail")
    @ResponseBody
    public Map<String,Object> fail(Integer chcId){
        Map<String,Object> modelMap = new HashMap<>();
        System.out.println("同意申请开发失败="+chcId);
        SalChance salChance = new SalChance();
        salChance.setChcId(Long.valueOf(chcId));
        salChance.setChcStatus("5");
        int result = salChanceService.updateSalChanceByChcId(salChance);
        if (result > 0){
            modelMap.put("success",true);
        }else {
            modelMap.put("success",false);
        }
        return modelMap;
    }

    @RequestMapping(value = "/noFail")
    @ResponseBody
    public Map<String,Object> noFail(Integer chcId){
        Map<String,Object> modelMap = new HashMap<>();
        SalChance salChance = new SalChance();
        salChance.setChcId(Long.valueOf(chcId));
        salChance.setChcStatus("2");
        int result = salChanceService.updateSalChanceByChcId(salChance);
        if (result > 0){
            modelMap.put("success",true);
        }else {
            modelMap.put("success",false);
        }
        return modelMap;
    }
}
